package com.sea.base.widget

import android.content.Context
import android.graphics.drawable.GradientDrawable.OVAL
import android.util.AttributeSet
import androidx.annotation.ColorInt
import androidx.core.view.setPadding
import com.custom.widget.R
import com.sea.base.ext.global.dp
import com.sea.base.ext.view.setTintColorInt
import com.sea.base.widget.shape.ShapeImageView

/**
 * 暂停播放的view，默认有5.dp的padding，你可以设置padding来覆盖
 * [playType]：播放中还是暂停中
 * [setColorInfo]：圆形颜色和按钮颜色
 */
class PlayPauseImageView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
    ShapeImageView(context, attrs, defStyleAttr) {
    companion object {
        const val PLAY_TYPE_PLAYING = 0
        const val PLAY_TYPE_PAUSE_ING = 1
    }

    /**
     * 设置播放状态
     * 播放中：暂停样式
     * 暂停中：播放样式
     */
    var playType: Int = PLAY_TYPE_PLAYING
        set(value) {
            field = value
            when (value) {
                PLAY_TYPE_PLAYING -> setImageResource(com.sea.base.R.drawable.common_ic_pause_white)
                PLAY_TYPE_PAUSE_ING -> setImageResource(com.sea.base.R.drawable.common_ic_play_white)
            }
        }

    init {
        if (attrs != null) {
            val a = context.obtainStyledAttributes(attrs, R.styleable.PlayPauseImageView)
            if (!a.hasValue(R.styleable.PlayPauseImageView_android_shape)) {
                val color = a.getColorStateList(R.styleable.PlayPauseImageView_shapeSolidColor)
                buildShape().apply {
                    setShape(OVAL)
                    setSolidColorStateList(color)
                }
            }
            playType = a.getInt(R.styleable.PlayPauseImageView_playType, PLAY_TYPE_PAUSE_ING)
            a.recycle()
        } else {
            //设置为默认
            playType = playType
        }
        if (paddingStart == 0 && paddingEnd == 0 && paddingLeft == 0 && paddingRight == 0 && paddingTop == 0 && paddingBottom == 0) {
            setPadding(5.dp)
        }
        scaleType = ScaleType.CENTER
    }

    fun setColorInfo(@ColorInt roundColorInt: Int, @ColorInt buttonColorInt: Int) {
        buildShape().setSolidColorInt(roundColorInt)
        setTintColorInt(buttonColorInt)
    }
}